package com.taxi.driver.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taxi.driver.R;


/**
 * 可扩展/收缩TextView
 */
public class ScalingTextView extends RelativeLayout implements View.OnClickListener {
    private TextView tv_title,tv_content;
    private ImageView iv_arrow;
    private LinearLayout ll_arrow;
    private final int DEFAULT_LINE = 0;// 默认展示最大行数3行
    private final int SHOW_CONTENT_NONE_STATE = 0;// 扩充
    private final int SHRINK_UP_STATE = 1;// 收起状态
    private final int SPREAD_STATE = 2;// 展开状态
    private int state = SHRINK_UP_STATE;//默认收起状态
    private OnClickListener listener;

    public ScalingTextView(Context context) {
        this(context, null);
    }

    public ScalingTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScalingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_scaling_text_view, null);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setMaxLines(DEFAULT_LINE);
        tv_content.requestLayout();
        iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
        ll_arrow = (LinearLayout) view.findViewById(R.id.ll_arrow);
        ll_arrow.setOnClickListener(this);
        addView(view);
        close();
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }

    public void setContent(String content){
        tv_content.setText(content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_arrow:
                if (state == SHRINK_UP_STATE) {
                    //open();
                } else if (state == SPREAD_STATE) {
                    //close();
                }
                if(listener !=null)
                    listener.onClick();
                break;
        }
    }

    public void close(){
        tv_content.setVisibility(GONE);
        tv_content.setMaxLines(DEFAULT_LINE);
        tv_content.requestLayout();
        iv_arrow.setRotation(0);
        state = SHRINK_UP_STATE;
    }

    public void open(){
        tv_content.setVisibility(VISIBLE);
        tv_content.setMaxLines(Integer.MAX_VALUE);
        tv_content.requestLayout();
        iv_arrow.setRotation(180);
        state = SPREAD_STATE;
    }

    public interface OnClickListener{
        void onClick();
    }

    public void setOnClickListener(OnClickListener listener){
        this.listener=listener;
    }
}
